/* 
 * Copyright 2014 Josef Hertl.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package caetus.modelChecking.transition;


/**
 *
 * @author Josef Hertl
 */
public class TransitionMaker {

    /**
     * Prepares the transition based off the input ordinal, state number,
     * command (=trigger) and following actions.
     *
     * @param ordinal The ordinal number of the transition.
     * @param state The state number - the state in which the transition can
     * occur.
     * @param command The variable that triggers the transition.
     * @param actions The array with actions the transition will perform when it
     * fires.
     * @return hlpsl-ready transition.
     */
    public static String makeTransition(int ordinal, int state, TransitionAction command, TransitionAction[] actions) {
        String result = "	" + ordinal + ". ";
        TransitionAction stateAction = new TransitionAction(TransitionActionEnum.IS_IN_STATE, null, state, false);
        result += stateAction.perform();
        result += makeAnd();
        result += command.perform();
        result += makeTransitionArrow();
        for (int i = 0; i < actions.length; i++) {
            if (i == 0) {
                result += "	   ";
            } else {
                result += makeAnd();
            }
            result += actions[i].perform();
        }
        result += "\n\n";
        return result;
    }
    
    /**
     * The transition is always made from state one.
     * @return The hlpsl-ready transition string.
     */
    public static String makeStateOneTransition(int ordinal, TransitionAction command, TransitionAction[] actions) {
        String result = "	" + ordinal + ". ";
        result += "State = 1";
        result += makeAnd();
        result += command.perform();
        result += makeTransitionArrow();
        for (int i = 0; i < actions.length; i++) {
            if (i == 0) {
                result += "	   ";
            } else {
                result += makeAnd();
            }
            result += actions[i].perform();
        }
        result += "\n\n";
        return result;
    }
    
    /**
     * The transition is made without any regards for states.
     * @return The hlpsl-ready transition string.
     */
    public static String makeStatelessTransition(int ordinal, TransitionAction command, TransitionAction[] actions) {
        String result = "	" + ordinal + ". ";
        result += command.perform();
        result += makeTransitionArrow();
        for (int i = 0; i < actions.length; i++) {
            if (i == 0) {
                result += "	   ";
            } else {
                result += makeAnd();
            }
            result += actions[i].perform();
        }
        result += "\n\n";
        return result;
    }
    
    private static String makeAnd() {
        return " /\\ ";
    }

    private static String makeTransitionArrow() {
        return " =|>\n";
    }
}
